﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Entity;
using CMS.BLL;

public partial class Partners_Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindType();
            string strPage = Request["page"];
            int page = 1;
            ViewState["title"] = "";
            ViewState["selectedtype"] = "";
            try
            {
                page = int.Parse(strPage);
                if (page < 1)
                {
                    page = 1;
                }
            }
            catch (Exception ex)
            {
                page = 1;
            }
            GetDataList(page);
        }
    }

    protected void BindType()
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        //先获取父id、
        string pid = "";
        if (dal.NewsType.Where(x => x.name == "合作伙伴").ToArray().Length > 0)
            pid = dal.NewsType.Where(x => x.name == "合作伙伴").Select(x => x.id).First();
        else
        {
            ClientScript.RegisterClientScriptBlock(GetType(), "aa", "alert('暂无合作伙伴类别，请至新闻类型中添加！',document.location='default.aspx');", true);
            return;
        }
        //string pid = System.Configuration.ConfigurationManager.AppSettings["PartnerTypeId"].ToString();
        ddlPid.DataSource = dal.NewsType.Where(x => x.pid == pid).Select(x => new { x.id, x.name }).ToArray();

        ddlPid.DataValueField = "id";
        ddlPid.DataTextField = "name";
        ddlPid.DataBind();
        ddlPid.Items.Insert(0, new ListItem("--请选择类别--", ""));
    }

    public void GetDataList(int page)
    {
        PartnerBLL bll=new PartnerBLL();
        ViewState["title"] = txtName.Value.ToString();
        int count = 0;
        List<PartnerEnty> list = bll.GetLst(page, ViewState["title"].ToString(), ViewState["selectedtype"].ToString(), out count);
        Repeater1.DataSource = list;
        Repeater1.DataBind();

        AspNetPager1.RecordCount = count;
        AspNetPager1.CurrentPageIndex = page;
    }

    public int GetPage()
    {
        return AspNetPager1.CurrentPageIndex;
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem
         || e.Item.ItemType == ListItemType.Item)
        {
            string typeid = (e.Item.FindControl("lblType") as Label).Text;

            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            if (dal.NewsType.Where(x => x.id == typeid).ToArray().Length > 0)
            {
                string name = dal.NewsType.Where(x => x.id == typeid).Select(x => x.name).First();
                (e.Item.FindControl("lblType") as Label).Text = name;
            }
            else
                (e.Item.FindControl("lblType") as Label).Text = typeid;

        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        GetDataList(AspNetPager1.CurrentPageIndex);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["selectedtype"] = ddlPid.SelectedValue;
        GetDataList(AspNetPager1.CurrentPageIndex);
    }
}

